package com.mls.guardian;


import com.mls.guardian.appst.AppController;
import com.mls.guardian.common.RemoteUtil;
import com.mls.guardian.config.AppSTConfig;
import com.mls.guardian.config.MasterConfig;
import com.mls.guardian.config.NettyConfig;
import com.mls.guardian.config.SlaveConfig;
import com.mls.guardian.config.helper.ConfigPathManagr;
import com.mls.guardian.config.helper.PropConfigHelper;
import com.mls.guardian.container.BaseContainer;
import com.mls.guardian.container.MasterContainer;
import com.mls.guardian.container.SlaveContainer;
import com.mls.guardian.remoting.netty.RemoteTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by jl on 15/9/9.
 */
public class MasterStartup {
    private static final Logger log = LoggerFactory.getLogger(MasterStartup.class);
    private static MasterConfig masterConfig = new MasterConfig();
    private static AppSTConfig appStConfig = new AppSTConfig();
    private static SlaveConfig slaveConfig = new SlaveConfig();
    private static NettyConfig nettyConfig = new NettyConfig();
    private final static String CONFIG_FOLDER = "guardianConfig";

    public static void main(String[] args) throws Exception{
        startup(args);
    }

    private static void startup(String args[]) throws Exception{
        String roles = args[0];
        final BaseContainer container;

        //生成配置文件目录
        if(!Paths.get(System.getProperty("user.home")+ File.separator+CONFIG_FOLDER).toFile().isDirectory()){
            Paths.get(System.getProperty("user.home")+ File.separator+CONFIG_FOLDER).toFile().mkdir();
        }


        //获取配置信息
        PropConfigHelper propConfigHelper = new PropConfigHelper();
        Properties pros = (java.util.Properties) propConfigHelper.parse(ConfigPathManagr.getConfigPath());
        PropConfigHelper.properties2Object(pros, masterConfig);
        PropConfigHelper.properties2Object(pros,appStConfig);
        PropConfigHelper.properties2Object(pros,slaveConfig);
        PropConfigHelper.properties2Object(pros,nettyConfig);


        if(roles.equals("MASTER")){
            container = new MasterContainer(masterConfig,slaveConfig,appStConfig,nettyConfig);
        }else{
            container = new SlaveContainer(masterConfig,slaveConfig,appStConfig,nettyConfig);
        }



        //初始化app内容
        AppController appController = new AppController(appStConfig);
        container.setAppController(appController);


        container.startup();

        log.info("local ip="+ RemoteUtil.getLocalHostIP());
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            private volatile boolean hasShutdown = false;
            private AtomicInteger shutdownTimes = new AtomicInteger(0);


            @Override
            public void run() {
                synchronized (this) {
                    log.info("shutdown hook was invoked, " + this.shutdownTimes.incrementAndGet());
                    if (!this.hasShutdown) {
                        this.hasShutdown = true;
                        long begineTime = System.currentTimeMillis();
                        container.shutdown();
//                        controller.shutdown();
                        long consumingTimeTotal = System.currentTimeMillis() - begineTime;
                        log.info("shutdown hook over, consuming time total(ms): " + consumingTimeTotal);
                    }
                }
            }
        }, "ShutdownHook"));
    }
}
